package com.hanqi.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanqi.entity.SysMenu;
import com.hanqi.entity.SysUser;
import com.hanqi.service.SysMenuFuncService;
import com.hanqi.service.SysMenuService;
import com.hanqi.util.PinyinUtil;
import com.hanqi.util.R;
import com.hanqi.util.UUIDGenerator;
import com.hanqi.util.shiro.ShiroUtil;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zjk
 * @since 2019-04-27
 */
@Controller
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Autowired
    protected SysMenuService sysMenuService;

    @Autowired
    protected SysMenuFuncService sysMenuFuncService;

    @RequestMapping("/index")
    public String index() {
        return "page/menuList";
    }

    @ResponseBody
    @RequestMapping("/list")
    public List<SysMenu> list() {
        SysUser user = ShiroUtil.getUserEntity();
        if (user == null) {
            return null;
        }
        return sysMenuService.listByUserId(user.getId());
    }

    @ResponseBody
    @RequestMapping("/manage/list")
    public List<SysMenu> manageList() {
        SysUser user = ShiroUtil.getUserEntity();
        if (user == null) {
            return null;
        }
        return sysMenuService.listAllByUserId(user.getId());
    }

    @ResponseBody
    @RequestMapping("/saveOrUpdate")
    public JSONObject saveOrUpdate(SysMenu sysMenu) {
        boolean r;
        if (StringUtils.isEmpty(sysMenu.getId())) {
            sysMenu.setId(UUIDGenerator.getUUID());
            String menuName = sysMenu.getMenuName();
            sysMenu.setMenuCode(PinyinUtil.getPingYin(menuName));
            sysMenu.setCreateDate(LocalDateTime.now());
            sysMenu.setCreateName(ShiroUtil.getUserEntity().getRname());
            sysMenu.setDeleteFlag(0);
            r = sysMenuService.save(sysMenu);
        } else {
            String menuName = sysMenu.getMenuName();
            String newCode = PinyinUtil.getPingYin(menuName);
            sysMenu.setMenuCode(newCode);
            r = sysMenuService.updateById(sysMenu);
            sysMenuFuncService.updateMenuCodeById(sysMenu.getId(), newCode);
        }
        return R.msg(r);
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JSONObject delete(String id) {
        boolean r = sysMenuService.removeById(id);
        return R.msg(r);
    }

    @ResponseBody
    @RequestMapping("/update")
    public JSONObject update(SysMenu menu) {
        if (StringUtils.isEmpty(menu.getId())) {
            return R.FAIL;
        }
        boolean r = sysMenuService.updateById(menu);
        return R.msg(r);
    }
    @ResponseBody
    @RequestMapping("/roleMenuData")
    public JSONObject roleMenuData(String roleId) {
        List<Map<String, Object>> list = sysMenuService.roleMenuData(roleId);
        if (list.isEmpty()) {
            return R.FAIL;
        }
        return R.msg(true, "", list);
    }
}
